package com.rd.p2p.mobile.user;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.CooperationLoginService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.CooperationLoginType;
import com.qdlc.p2p.dal.dto.CooperationLogin;
import com.qdlc.p2p.dal.dto.User;

/**
 * 登录的账号与微信绑定
 */
public class MobileCooperationLoginAction extends BaseAction<User> implements ModelDriven<User> {
	@Resource
	private CooperationLoginService cooperationLoginService;

	@Action("/mobile/cooperation/add")
	public void cooperationAdd() throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		User u = getSessionUser();
		if (u == null) {
			data.put("result", false);
			data.put("msg", "用户尚未登录！");
			printWebJson(getStringOfJpaObj(data));
			return;
		}
		String openId = (String) session.get("openId");
		if (StringUtils.isNotBlank(openId)) {
			CooperationLogin cooperation = new CooperationLogin();
			cooperation.setOpenId(openId);
			cooperation.setAddTime(new Date());
			cooperation.setUserId(u.getUserId());
			cooperation.setType(Integer.parseInt(CooperationLoginType.TYPE_WEIXIN));
			cooperationLoginService.addCooperationLogin(cooperation);
			data.put("result", true);
			data.put("msg", "新增成功！");
			printWebJson(getStringOfJpaObj(data));
		}
		return;
	}

}
